package cn.qingyun.gis.modules.express.controller;


import cn.hutool.core.date.DateUtil;
import cn.qingyun.gis.common.Result;
import cn.qingyun.gis.modules.express.entity.ExpressRegion;
import cn.qingyun.gis.modules.express.request.ExpressRegionRequest;
import cn.qingyun.gis.modules.express.service.IExpressRegionService;
import cn.qingyun.gis.modules.init.system.annotation.AutoLog;
import cn.qingyun.gis.modules.init.system.base.controller.BasicController;
import cn.qingyun.gis.modules.init.system.query.QueryGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * <p>
 * 快递业务规划数据 前端控制器
 * </p>
 *
 * @author miyunguo
 * @since 2023-08-28
 */
@RestController
@RequestMapping("/app/express-region")
public class ExpressRegionController extends BasicController<ExpressRegion, IExpressRegionService> {
    @Resource
    private IExpressRegionService iExpressRegionService;

    @PostMapping("/queryRegion")
    public Result<?> getRegionList(@RequestBody ExpressRegionRequest request) {
        return Result.okData(iExpressRegionService.getRegionList(request));
    }

    @GetMapping("/wkt84To02")
    @ApiOperation(value = "84转02", notes = "84转02")
    public Result<?> wkt84To02(){
        iExpressRegionService.wkt84To02();
        return Result.ok("正在将84 -> 02");
    }

    @GetMapping("/repairInvalid")
    @ApiOperation(value = "修复无效边界", notes = "修复无效边界")
    public Result<?> repairInvalidBoundary(){
        iExpressRegionService.repairInvalidBoundary();
        return Result.ok("正在修复边界");
    }

    // 后端管理

    /**
     * 分页列表查询
     *
     * @param expressRegion
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "快递业务规划数据表-分页列表查询")
    @ApiOperation(value = "快递业务规划数据表-分页列表查询", notes = "快递业务规划数据表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(ExpressRegion expressRegion,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<ExpressRegion> queryWrapper = QueryGenerator.initQueryWrapper(expressRegion, req.getParameterMap());
        queryWrapper.lambda().orderByDesc(ExpressRegion::getUpdateTime);
        Page<ExpressRegion> page = new Page<>(pageNo, pageSize);
        IPage<ExpressRegion> pageList = iExpressRegionService.page(page, queryWrapper);
        return Result.okData(pageList);
    }

    /**
     * 添加
     *
     * @param expressRegion
     * @return
     */
    @AutoLog(value = "快递业务规划数据表-添加")
    @ApiOperation(value = "快递业务规划数据表-添加", notes = "快递业务规划数据表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody ExpressRegion expressRegion) {
        iExpressRegionService.save(expressRegion);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param expressRegion
     * @return
     */
    @AutoLog(value = "快递业务规划数据表-编辑")
    @ApiOperation(value = "快递业务规划数据表-编辑", notes = "快递业务规划数据表-编辑")
    @PostMapping(value = "/edit")
    public Result<?> edit(@RequestBody ExpressRegion expressRegion) {
        expressRegion.setUpdateTime(DateUtil.date());
        iExpressRegionService.updateById(expressRegion);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "快递业务规划数据表-通过id删除")
    @ApiOperation(value = "快递业务规划数据表-通过id删除", notes = "快递业务规划数据表-通过id删除")
    @GetMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id") String id) {
        iExpressRegionService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "快递业务规划数据表-批量删除")
    @ApiOperation(value = "快递业务规划数据表-批量删除", notes = "快递业务规划数据表-批量删除")
    @GetMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.iExpressRegionService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "快递业务规划数据表-通过id查询")
    @ApiOperation(value = "快递业务规划数据表-通过id查询", notes = "快递业务规划数据表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id") String id) {
        ExpressRegion expressRegion = iExpressRegionService.getById(id);
        if (expressRegion == null) {
            return Result.error("未找到对应数据");
        }
        return Result.okData(expressRegion);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param expressRegion
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ExpressRegion expressRegion) {
        return super.exportXls(request, expressRegion, ExpressRegion.class, "快递业务规划数据表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ExpressRegion.class);
    }
}
